﻿using Elastic.Clients.Elasticsearch;
using Elastic.Transport;

namespace WebAPI.Utils;

/// <summary>
/// Elastic.Clients.Elasticsearch
/// </summary>
internal class ElasticsearchHelper
{
    private readonly ElasticsearchClient _client;

    public ElasticsearchHelper()
    {
        //password: T-0oFKMZ2B-8fqXh7HUn
        //fingerprint: d1351f3b9291351933f21e9cfe6c49a0342d90a5f57b807ac09c862dec1fa965
        //token：eyJ2ZXIiOiI4LjYuMiIsImFkciI6WyIxOTIuMTY4LjAuMjQwOjkyMDAiXSwiZmdyIjoiZDEzNTFmM2I5MjkxMzUxOTMzZjIxZTljZmU2YzQ5YTAzNDJkOTBhNWY1N2I4MDdhYzA5Yzg2MmRlYzFmYTk2NSIsImtleSI6ImxPZ0hUWXNCaWY1NU5LdDE3TUdtOkxlVFF5djZuUTNtY2htTzhCdHVWYncifQ==

        var settings = new ElasticsearchClientSettings(new Uri("http://localhost:9200"))
            .CertificateFingerprint("d1351f3b9291351933f21e9cfe6c49a0342d90a5f57b807ac09c862dec1fa965")
            .Authentication(new BasicAuthentication("elastic", "T-0oFKMZ2B-8fqXh7HUn"));

        _client = new ElasticsearchClient(settings);
    }

    /// <summary>
    /// 创建索引
    /// </summary>
    /// <param name="index"></param>
    public async Task Index(string index)
    {
        var response = await _client.Indices.CreateAsync(index);
    }
    
    /// <summary>
    /// 创建
    /// </summary>
    /// <param name="index"></param>
    /// <param name="document"></param>
    /// <returns></returns>
    public async Task Index(string index, object document)
    {
        var indexResponse = await _client.IndexAsync(document);
        if (indexResponse.IsValidResponse)
        {
            Console.WriteLine($"Index document with ID {indexResponse.Id} succeeded.");
        }
    }

    /// <summary>
    /// 获取
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="index"></param>
    /// <param name="id"></param>
    /// <returns></returns>
    public async Task Get<T>(string index, string id)
    {
        var response = await _client.GetAsync<T>(id, idx => idx.Index(index));
        if (response.IsValidResponse)
        {
            var source = response.Source;
            Console.WriteLine(source);
        }
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="index"></param>
    /// <param name="id"></param>
    /// <returns></returns>
    public async Task Delete(string index, string id)
    {
        // var deleteResponse = await _client.DeleteAsync(index, id);
        // if (deleteResponse.IsValidResponse)
        // {
        //     Console.WriteLine("Delete document succeeded.");
        // }
    }

    /// <summary>
    /// 更新
    /// </summary>
    /// <param name="index"></param>
    /// <param name="id"></param>
    /// <param name="document"></param>
    /// <returns></returns>
    public async Task Update<T>(string index, string id, T document)
    {
        var updateResponse = await _client.UpdateAsync<T, T>(index, id, u => u.Doc(document));
        if (updateResponse.IsValidResponse)
        {
            Console.WriteLine("Update document succeeded.");
        }
    }

    /// <summary>
    /// 搜索
    /// </summary>
    /// <param name="index"></param>
    /// <param name="document"></param>
    /// <returns></returns>
    public async Task Search<T>(string index, T document)
    {
        // var searchResponse = await _client.SearchAsync<T>(s => s
        //     .Index(index)
        //     .From(0)
        //     .Size(10)
        //     .Query(q => q.Term(t => document, "admin")// 字段，值
        //     )
        // );
        // if (searchResponse.IsValidResponse)
        // {
        //     var doc = searchResponse.Documents.FirstOrDefault();
        //     Console.WriteLine();
        // }
    }
}